package com.opennews.openplatform.controller;

import com.opennews.openplatform.common.constant.IncomeExpenseTypeConstant;
import com.opennews.openplatform.dto.income.expense.detailt.ype.*;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.IncomeExpenseDetailTypeService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/income-expense-detail-type")
public class IncomeExpenseDetailTypeController {
    private final IncomeExpenseDetailTypeService incomeExpenseDetailTypeService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/query-by-account-group")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryByAccountGroup(@RequestParam String incomeExpenseType) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        List<Map<String, Object>> result = incomeExpenseDetailTypeService.queryByAccountGroup(accountGroupId, incomeExpenseType);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/check-existing")
    public ResponseEntity<ApiResponse<Boolean>> checkExisting(@RequestParam String incomeExpenseType, @RequestParam String id, @RequestParam String name) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        boolean result = incomeExpenseDetailTypeService.checkExisting(accountGroupId, incomeExpenseType, id, name);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/query-analysis-not-included-detail-types")
    public ResponseEntity<ApiResponse<Map<String, Object>>> queryAnalysisNotIncludedDetailTypes() {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        List<Map<String, Object>> incomeDetailTypes = incomeExpenseDetailTypeService.queryAnalysisNotIncludedDetailTypes(accountGroupId, IncomeExpenseTypeConstant.INCOME);
        List<Map<String, Object>> expenseDetailTypes = incomeExpenseDetailTypeService.queryAnalysisNotIncludedDetailTypes(accountGroupId, IncomeExpenseTypeConstant.EXPENSE);

        return httpResponseHelper.getSuccessResponse(Map.of(
                "incomeDetailTypes", incomeDetailTypes,
                "expenseDetailTypes", expenseDetailTypes
        ));
    }

    @PostMapping("/reset-order")
    public ResponseEntity<ApiResponse<Object>> resetOrder(@Valid @RequestBody ResetOrderIncomeExpenseDetailTypeDto data) {
        List<String> detailTypeIds = data.getDetailTypeIds();
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        incomeExpenseDetailTypeService.resetOrder(accountGroupId, detailTypeIds);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse<Map<String, Object>>> add(@Valid @RequestBody CreateIncomeExpenseDetailTypeDto data) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        Map<String, Object> result = incomeExpenseDetailTypeService.add(data, accountGroupId);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/update")
    public ResponseEntity<ApiResponse<Map<String, Object>>> update(@Valid @RequestBody UpdateIncomeExpenseDetailTypeDto data) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        Map<String, Object> result = incomeExpenseDetailTypeService.update(data, accountGroupId);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/delete")
    public ResponseEntity<ApiResponse<Boolean>> delete(@Valid @RequestBody IncomeExpenseDetailTypeWithNameDto data) {
        String incomeExpenseType = data.getIncomeExpenseType();
        String name = data.getName();
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        boolean result = incomeExpenseDetailTypeService.delete(incomeExpenseType, name, accountGroupId);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/batch-delete")
    public ResponseEntity<ApiResponse<List<String>>> batchDelete(@Valid @RequestBody IncomeExpenseDetailTypeWithNamesDto data) {
        String incomeExpenseType = data.getIncomeExpenseType();
        List<String> names = data.getNames();
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        List<String> result = incomeExpenseDetailTypeService.delete(incomeExpenseType, names, accountGroupId);

        return httpResponseHelper.getSuccessResponse(result);
    }
}